package com.ruoyi.work.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.work.domain.MapNodes;
import com.ruoyi.work.mapper.MapRoadlinksMapper;
import com.ruoyi.work.domain.MapRoadlinks;
import com.ruoyi.work.service.IMapRoadlinksService;

/**
 * 路线Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-28
 */
@Service
public class MapRoadlinksServiceImpl implements IMapRoadlinksService 
{
    @Autowired
    private MapRoadlinksMapper mapRoadlinksMapper;

    /**
     * 查询路线
     * 
     * @param id 路线主键
     * @return 路线
     */
    @Override
    public MapRoadlinks selectMapRoadlinksById(Long id)
    {
        return mapRoadlinksMapper.selectMapRoadlinksById(id);
    }

    /**
     * 查询路线列表
     * 
     * @param mapRoadlinks 路线
     * @return 路线
     */
    @Override
    public List<MapRoadlinks> selectMapRoadlinksList(MapRoadlinks mapRoadlinks)
    {
        return mapRoadlinksMapper.selectMapRoadlinksList(mapRoadlinks);
    }

    /**
     * 新增路线
     * 
     * @param mapRoadlinks 路线
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMapRoadlinks(MapRoadlinks mapRoadlinks)
    {
        mapRoadlinks.setCreateTime(DateUtils.getNowDate());
        int rows = mapRoadlinksMapper.insertMapRoadlinks(mapRoadlinks);
        insertMapNodes(mapRoadlinks);
        return rows;
    }

    /**
     * 修改路线
     * 
     * @param mapRoadlinks 路线
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMapRoadlinks(MapRoadlinks mapRoadlinks)
    {
        mapRoadlinks.setUpdateTime(DateUtils.getNowDate());
        mapRoadlinksMapper.deleteMapNodesByLinkId(mapRoadlinks.getId());
        insertMapNodes(mapRoadlinks);
        return mapRoadlinksMapper.updateMapRoadlinks(mapRoadlinks);
    }

    /**
     * 批量删除路线
     * 
     * @param ids 需要删除的路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMapRoadlinksByIds(Long[] ids)
    {
        mapRoadlinksMapper.deleteMapNodesByLinkIds(ids);
        return mapRoadlinksMapper.deleteMapRoadlinksByIds(ids);
    }

    /**
     * 删除路线信息
     * 
     * @param id 路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMapRoadlinksById(Long id)
    {
        mapRoadlinksMapper.deleteMapNodesByLinkId(id);
        return mapRoadlinksMapper.deleteMapRoadlinksById(id);
    }


    /**
     * 新增节点信息
     * 
     * @param mapRoadlinks 路线对象
     */
    public void insertMapNodes(MapRoadlinks mapRoadlinks)
    {
        List<MapNodes> mapNodesList = mapRoadlinks.getMapNodesList();
        Long id = mapRoadlinks.getId();
        if (StringUtils.isNotNull(mapNodesList))
        {
            List<MapNodes> list = new ArrayList<MapNodes>();
            for (MapNodes mapNodes : mapNodesList)
            {
                mapNodes.setLinkId(id);
                list.add(mapNodes);
            }
            if (list.size() > 0)
            {
                mapRoadlinksMapper.batchMapNodes(list);
            }
        }
    }
    /**
     * 导入路线信息
     *
     * @param userList 路线列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importMapRoadlinks(List<MapRoadlinks> mapRoadlinksList, Boolean isUpdateSupport)
    {
        if (StringUtils.isNull(mapRoadlinksList) || mapRoadlinksList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MapRoadlinks mapRoadlinks : mapRoadlinksList)
        {
            try
            {
                this.insertMapRoadlinks(mapRoadlinks);
                successNum++;
                successMsg.append("<br/>" + successNum + "：" + mapRoadlinks.toString() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、姓名 " + mapRoadlinks.toString() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                // log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
